package aio.server;

import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

/**
 * @author Huhailong
 * @Description
 * @Date 2021/4/12.
 */
public class AcceptCompletionHandler implements CompletionHandler<AsynchronousSocketChannel,TimeServerHandle> {
    @Override
    public void completed(AsynchronousSocketChannel result, TimeServerHandle attachment) {
        attachment.serverSocketChannel.accept(attachment,this);
        System.out.println("accept second client");
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        result.read(buffer,buffer, new ReadCompletionHandler(result));
    }

    @Override
    public void failed(Throwable exc, TimeServerHandle attachment) {
        exc.printStackTrace();
        attachment.latch.countDown();
    }
}
